package com.idbk.chargestation.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.pile.ActivityPointList;
import com.idbk.chargestation.db.SearchHistoryDatabase;

import java.util.List;

/**
 * 关键字搜索适配器
 * Created by zhx on 2017/5/8.
 */

public class SearchHistoryAdapter extends BaseAdapter {

    private Context mContext;

    private List<SearchHistoryDatabase.KeywordItem> mSearchData;

    private SearchHistoryDatabase mDb;

    public SearchHistoryAdapter(
            Context context,
            List<SearchHistoryDatabase.KeywordItem> data,
            SearchHistoryDatabase db) {
        mContext = context;
        mSearchData = data;
        mDb = db;
    }

    @Override
    public int getCount() {
        return mSearchData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        ViewHold hold;
        if (convertView == null) {
            v = LayoutInflater.from(mContext).inflate(R.layout.item_search_history, parent, false);
            hold = new ViewHold();
            hold.mParent = v;
            hold.mTextWord = (TextView) v.findViewById(R.id.textview_search_item_keyword);
            hold.mImageDelete = (ImageView) v.findViewById(R.id.imageview_search_item_delete);
            v.setTag(hold);
        } else {
            v = convertView;
            hold = (ViewHold) v.getTag();
        }

        bindView(position, hold);

        return v;
    }

    private void bindView(final int position, ViewHold hold) {
        final SearchHistoryDatabase.KeywordItem item = mSearchData.get(position);

        hold.mParent.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ActivityPointList.class);
                intent.putExtra(ActivityPointList.KEY_MODEL, ActivityPointList.MODEL_KEYWORD);
                intent.putExtra(ActivityPointList.KEY_KEYWORD, item.keyword);
                mContext.startActivity(intent);
            }
        });
        hold.mTextWord.setText(item.keyword);
        hold.mImageDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDb.deleteOne(item.id);
                mSearchData.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    private class ViewHold {
        private View mParent;
        private TextView mTextWord;
        private ImageView mImageDelete;
    }

}

